<template>
    <div>
        <div class="searchHeader">
            <el-input v-model="keyWords" placeholder="请输入搜索关键字..." class="articleSearchText" size="mini"></el-input>
            <el-button @click="listCategories" size="mini" type="primary" icon="el-icon-search">搜索文章类别</el-button>
            <el-button size="mini" type="primary" icon="el-icon-plus" @click="dialogVisibleOfInsert=true">新增文章类别</el-button>
        </div>
        <div>
            <el-table
                    border
                    @selection-change="selectionChange"
                    v-loading="loading"
                    :data="cateList">
                <el-table-column
                        type="selection"
                        width="55">
                </el-table-column>
                <el-table-column
                        label="编号">
                    <template slot-scope="scope">{{ scope.row.id }}</template>
                </el-table-column>
                <el-table-column
                        prop="cname"
                        label="栏目名称">
                </el-table-column>
                <el-table-column
                        prop="createTime"
                        label="创建时间">
                </el-table-column>
                <el-table-column
                        label="栏目状态">
                    <template slot-scope="scope">
                        <el-switch
                                @change="updateDelmark($event,scope.row.id)"
                                active-color="#13ce66"
                                inactive-color="#ff4949"
                                v-model="scope.row.delMark"
                                active-text="启用"
                                inactive-text="禁用">
                        </el-switch>
                    </template>
                </el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button
                                size="mini"
                                @click="handleEdit(scope.row)">编辑
                        </el-button>
                        <el-button
                                size="mini"
                                type="danger"
                                @click="handleDelete(scope.row)">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <br>
        <el-button size="mini" type="danger" :disabled="disabled" @click="multiDelCategories">批量删除</el-button>
        <!-- 新增栏目 Dialog -->
        <el-dialog
                title="新增文章栏目"
                :visible.sync="dialogVisibleOfInsert"
                width="30%">
            <div>
                <el-tag>栏目名称</el-tag>
                <el-input v-model="categoryData.cname" style="width: 70%;margin-left: 8px" autofocus size="small" placeholder="新增栏目名称"></el-input>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button size="mini" @click="hideAddDialog">取 消</el-button>
                <el-button size="mini" type="primary" @click="insertCategory">确 定</el-button>
            </span>
        </el-dialog>
        <!-- 编辑栏目 Dialog -->
        <el-dialog
                title="编辑文章栏目"
                :visible.sync="dialogVisibleOfUpdate"
                width="30%">
            <div>
                <el-tag>栏目名称</el-tag>
                <el-input v-model="categoryData.cname" style="width: 70%;margin-left: 8px" autofocus size="small" placeholder="新增栏目名称"></el-input>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button size="mini" @click="hideUpdateDialog">取 消</el-button>
                <el-button size="mini" type="primary" @click="updateCategory">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "CategoryManage",
        data() {
            return {
                dialogVisibleOfUpdate : false,
                categoryData : { // 新增 更新 栏目对象
                    id : -1,
                    cname : ''
                },
                dialogVisibleOfInsert:false, // 新增栏目 Dialog 是否可见
                categoryStatus: {},// 修改栏目的id和状态
                delSelections: [],// 批量删除的数据
                disabled: true, // 批量删除禁用效果
                loading: true, // 加载效果
                cateList: [],// 类别信息
                keyWords: '',// 搜索关键字
            };
        },
        methods: {
            updateCategory() { // 修改栏目名称
                if (this.categoryData.cname.trim() == '') {
                    this.$message.warning('栏目名称不能为空');
                }else{
                    this.$putRequest('/category/',this.categoryData).then(resp => {
                        if (resp) {
                            this.listCategories();
                            this.hideUpdateDialog();
                        }
                    })
                }
            },
            hideUpdateDialog() {// 隐藏编辑栏目Dialog
                this.dialogVisibleOfUpdate = false;
                this.categoryData.cname = '';
                this.categoryData.id = -1;
            },
            handleEdit(row) { // 显示 编辑 Dialog
                this.dialogVisibleOfUpdate = true;
                Object.assign(this.categoryData, row);
            },
            insertCategory() { // 新增栏目
                if (this.categoryData.cname.trim() == '') {
                    this.$message.warning('栏目名称不能为空');
                }else{
                    this.$postRequest('/category/',this.categoryData).then(resp => {
                        if (resp) {
                            this.listCategories();
                            this.hideAddDialog();
                        }
                    })
                }
            },
            hideAddDialog() { // 隐藏新增栏目Dialog
                this.dialogVisibleOfInsert = false;
                this.categoryData.cname = '';
                this.categoryData.id = -1;
            },
            updateDelmark(delMark, id) { // 修改栏目状态
                this.categoryStatus.delMark = delMark;
                this.categoryStatus.id = id;
                this.$putRequest('/category/updateCategoryDelMark', this.categoryStatus).then(resp => {
                })
            },
            multiDelCategories() { // 批量删除
                this.$confirm('此操作将永久删除【' + this.delSelections.length + '】条栏目, 是否继续?', '删除', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let ids = '?';
                    this.delSelections.forEach((item) => {
                        ids += 'ids=' + item.id + '&';
                    })
                    this.$delRequest('/category/multiDel' + ids).then(resp => {
                        this.listCategories();
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            handleDelete(row) { // 根据 id 删除栏目
                this.$confirm('此操作将永久删除【' + row.cname + '】栏目, 是否继续?', '删除', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$delRequest('/category/' + row.id).then(resp => {
                        this.listCategories();
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            selectionChange(selection) { // 复选框回调
                this.delSelections = selection;
                this.disabled = selection.length == 0;
            },
            listCategories() { // 初始化类别信息
                this.$getRequest('/category/?keyWords=' + this.keyWords).then(resp => {
                    if (resp) {
                        this.cateList = resp;
                        this.loading = false;
                    }
                })
            }
        },
        mounted() {
            this.listCategories();
        }
    }
</script>

<style scoped>
    .searchHeader .articleSearchText {
        width: 300px;
        margin-bottom: 10px;
        margin-right: 10px;
    }
</style>